package com.stylefeng.guns.wx.modular.scheduled.service.impl;/**
 * Package: com.stylefeng.guns.webmagic
 * Description： TODO
 * Author: 4967677099@qq.com
 * Date: Created in 2018/3/18 0018 16:35
 * Version: 0.0.1
 * Modified By: 4967677099@qq.com
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.wx.common.persistence.dao.UserNovelMapper;
import com.stylefeng.guns.wx.common.persistence.model.UserNovel;
import com.stylefeng.guns.wx.modular.scheduled.service.IUserNovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.*;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 496767099@qq.com
 * @create 2018-03-18 16:35
 **/
@Component("WebmagicQiDianPipeline")
public class WebmagicQiDianPipeline implements Pipeline {
    @Autowired
    UserNovelMapper userNovelMapper;
    @Override
    public void process(ResultItems resultItems, Task task) {
        String title = resultItems.getAll().get("title").toString();
        String latestChapter = resultItems.getAll().get("latestChapter").toString();
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("novel_title", title);
        //Page<UserNovel> page2 = new PageFactory<UserNovel>().OneNumPage(1);
        List<UserNovel> userNovelList = new ArrayList<>();
        userNovelList = userNovelMapper.selectList(wrapper);
        if(null!=userNovelList&&userNovelList.size()!=0){
            UserNovel userNovel = userNovelList.get(0);
            if(!userNovel.getNovelLatestChapter().equals(latestChapter)){
                Wrapper<UserNovel> wrapper2 = new EntityWrapper<>();
                wrapper2 = wrapper2.eq("novel_title", title)
                .notIn("novel_latest_chapter",latestChapter);
                UserNovel u = new UserNovel();
                u.setSendFlag("0");
                u.setNovelLatestChapter(latestChapter);
                userNovelMapper.update(u,wrapper2);
            }
        }
    }
}
